package com.ship.dispatch.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ship.common.core.domain.R;
import com.ship.common.core.exception.BusException;
import com.ship.common.core.web.domain.BaseResultCode;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.business.service.impl.BaseServiceImpl;
import com.ship.common.core.utils.BeanCopyUtils;
import com.ship.common.security.utils.SecurityUtils;
import com.ship.dispatch.bean.*;
import com.ship.dispatch.bean.treaShipNetwork.IhsShipBase;
import com.ship.dispatch.bo.SmShipCrewImportBo;
import com.ship.dispatch.bpm.service.ProcessInstanceService;
import com.ship.dispatch.mapper.SpInformationListMapper;
import com.ship.dispatch.service.*;
import com.ship.dispatch.mapper.SmShipCrewMapper;
import com.ship.system.api.BpmInstanceService;
import org.apache.commons.collections4.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
* @author zhangyh
* @description 针对表【sm_ship_crew(船舶船员信息主表)】的数据库操作Service实现
* @createDate 2024-01-16 16:11:00
*/
@Service
public class SmShipCrewServiceImpl extends BaseServiceImpl<SmShipCrewMapper, SmShipCrew>
    implements SmShipCrewService{

    @Autowired
    private SmShipCrewDetailService smShipCrewDetailService;

    @Autowired
    private SmShipCrewRecordService smShipCrewRecordService;

    @Autowired
    private SmShipCrewDetailChangeService smShipCrewDetailChangeService;

    @Autowired
    private ConShippingService conShippingService;

    @Autowired
    private SpInformationListMapper informationListMapper;

    public static Map<String,String> monthMap = new HashedMap<>();

    static {
        monthMap.put("Jan","01");
        monthMap.put("Feb","02");
        monthMap.put("Mar","03");
        monthMap.put("Apr","04");
        monthMap.put("May","05");
        monthMap.put("Jun","06");
        monthMap.put("Jul","07");
        monthMap.put("Aug","08");
        monthMap.put("Sep","09");
        monthMap.put("Oct","10");
        monthMap.put("Nov","11");
        monthMap.put("Dec","12");

        monthMap.put("一月","01");
        monthMap.put("二月","02");
        monthMap.put("三月","03");
        monthMap.put("四月","04");
        monthMap.put("五月","05");
        monthMap.put("六月","06");
        monthMap.put("七月","07");
        monthMap.put("八月","08");
        monthMap.put("九月","09");
        monthMap.put("十月","10");
        monthMap.put("十一月","11");
        monthMap.put("十二月","12");
    }

    @Override
    public JsonResult baseImport(SmShipCrewImportBo bo) {
        Long conId = bo.getConId();
        String shipName = bo.getShipName();
        String fileUrl = bo.getFileUrl();
        List<SmShipCrewDetail> shipCrewDetailList = new ArrayList<>();
        if(conId == null && StringUtils.isBlank(shipName)){
            return JsonResult.failed("参数信息异常");
        }

        List<SmShipCrewDetail> originDetailList = new LinkedList<>();
        String imo = null;
        if(conId != null){
            ConShipping conShipping = conShippingService.getById(conId);
            if(conShipping == null){
                return JsonResult.failed("船运合同信息异常");
            }
            shipName = conShipping.getShipName();
            imo = conShipping.getImo();
        }

        SmShipCrew smShipCrew = this.baseMapper.selectOne(
                new LambdaQueryWrapper<SmShipCrew>()
                        .eq(SmShipCrew::getShipName,shipName)
                        .last("limit 1")
        );

        if(StringUtils.isNotBlank(fileUrl)){
            try {
                URL url = new URL(fileUrl);
                InputStream inputStream = url.openStream();
                // 从MultipartFile创建ExcelReaderBuilder
                ExcelReaderBuilder excelReaderBuilder = EasyExcel.read(inputStream);
                // 选择要读取的Sheet，并忽略头部
                ExcelReaderSheetBuilder sheetBuilder = excelReaderBuilder.sheet().headRowNumber(10);
                // 读取整个Sheet的数据
                List<Map<Integer,String>> allRows = sheetBuilder.doReadSync();
                shipCrewDetailList = formatShipCrewExcelData(allRows);
            } catch (Exception e) {
                return JsonResult.failed("船员文件解析异常");
            }
            if(shipCrewDetailList.size() == 0){
                return JsonResult.success("共导入0个船员信息");
            }
        }else{
            if(smShipCrew != null){
                return JsonResult.failed("该船只信息已存在，请找到对应船只后，点击查看进入详情页编辑");
            }
        }

        if(smShipCrew == null){
            smShipCrew = new SmShipCrew();
            smShipCrew.setShipName(shipName);
            smShipCrew.setImo(imo);
            //添加船旗国和呼号
            if(StringUtils.isNotBlank(imo)){
                IhsShipBase shipBase = informationListMapper.getShipInfoByImo(imo);
                if(null != shipBase){
                    smShipCrew.setCallSign(shipBase.getCallsign());
                    smShipCrew.setFlagState(shipBase.getFlagName());
                }
            }
            smShipCrew.setMasterName("");
            smShipCrew.setHighCrewNum(0);
            smShipCrew.setCrewNum(0);
            smShipCrew.setCreateBy(SecurityUtils.getUsername());
            smShipCrew.setUpdatedBy(SecurityUtils.getUsername());
            this.baseMapper.insert(smShipCrew);
        }

        //校验船员证是否在其他船只下已存在
        List<String> passportNos = shipCrewDetailList.stream().map(SmShipCrewDetail::getPassportNo).collect(Collectors.toList());
        Long repeatCount = smShipCrewDetailService.getBaseMapper().selectCount(
                new LambdaQueryWrapper<SmShipCrewDetail>()
                        .in(SmShipCrewDetail::getPassportNo,passportNos)
                        .ne(SmShipCrewDetail::getShipCrewId,smShipCrew.getId())
        );
        if(repeatCount > 0){
            return JsonResult.failed("当前导入的船员信息已存在于其他船只，请核查");
        }

        originDetailList = smShipCrewDetailService.getBaseMapper().selectList(
                new LambdaQueryWrapper<SmShipCrewDetail>()
                        .eq(SmShipCrewDetail::getShipCrewId,smShipCrew.getId())
                        .eq(SmShipCrewDetail::getIsTemp,"0")
        );

        int onCount = 0;
        int offCount = 0;
        int failCount = 0;
        List<SmShipCrewDetail> onCrewDetailList = new ArrayList<>();
        for (SmShipCrewDetail crewDetail : shipCrewDetailList) {
            if(StringUtils.isBlank(crewDetail.getCrewName()) || StringUtils.isBlank(crewDetail.getPassportNo())){
                failCount ++;
                continue;
            }
            //判断当前船员是否已存在（名字+船员证号）
            SmShipCrewDetail originCrew = smShipCrewDetailService.getBaseMapper().selectOne(
                    new LambdaQueryWrapper<SmShipCrewDetail>()
                    .eq(SmShipCrewDetail::getPassportNo,crewDetail.getPassportNo())
                    .last("limit 1")
            );
            if(originCrew != null && StringUtils.equals("1",originCrew.getStatus())){
                //如果该船员已经上船则不需要更新
                onCrewDetailList.add(originCrew);
                continue;
            }
            if(originCrew == null){
                //不存在则新增
                crewDetail.setShipCrewId(smShipCrew.getId());
                crewDetail.setIsHigh("1");
                crewDetail.setStatus("1");
                smShipCrewDetailService.save(crewDetail);
                onCrewDetailList.add(crewDetail);
                //更新船员上船记录
                updateCrewLoadRecord(crewDetail);
            }else{
                //存在则更新登船状态
                originCrew.setStatus("1");
                smShipCrewDetailService.updateById(originCrew);
                onCrewDetailList.add(originCrew);
                //更新船员上船记录
                updateCrewLoadRecord(originCrew);
            }
            onCount ++;
        }
        if(onCrewDetailList.size() == 0){
            return JsonResult.success("导入成功，上船：0个，离船0个，失败"+shipCrewDetailList.size()+"个（船员已上船或信息异常）");
        }
        Map<Long,List<SmShipCrewDetail>> onBoatMap = onCrewDetailList.stream().collect(Collectors.groupingBy(SmShipCrewDetail::getId));
        List<SmShipCrewDetail> offCrewDetailList = new ArrayList<>();
        for (SmShipCrewDetail crewDetail : originDetailList) {
            if(onBoatMap.get(crewDetail.getId()) == null){
                crewDetail.setStatus("0");
                offCrewDetailList.add(crewDetail);
                //更新船员离船记录
                updateCrewLoadRecord(crewDetail);
            }
        }
        if(offCrewDetailList.size() > 0){
            smShipCrewDetailService.updateBatchById(offCrewDetailList);
            offCount = offCrewDetailList.size();
        }

        //更新船只船员数量信息
        this.resetCrewCountInfo(smShipCrew);

        //增加操作记录
        SmShipCrewRecord crewRecord = new SmShipCrewRecord();
        crewRecord.setShipCrewId(smShipCrew.getId());
        crewRecord.setOperateType("1");
        crewRecord.setOperateRemark("船只【"+smShipCrew.getShipName()+"】下属船员excel批量导入");
        crewRecord.setOperateTime(new Date());
        crewRecord.setOperateBy(SecurityUtils.getUsername());
        smShipCrewRecordService.getBaseMapper().insert(crewRecord);
        return JsonResult.success("导入成功，上船："+onCount+"个，离船"+offCount+"个，失败"+failCount+"个");
    }

    @Override
    public void checkShipCrewStatus() {
        List<SmShipCrewDetail> detailList = smShipCrewDetailService.getBaseMapper().selectList(
                new LambdaQueryWrapper<SmShipCrewDetail>()
                        .eq(SmShipCrewDetail::getStatus,"1")
                        .lt(SmShipCrewDetail::getOffBoatDate,new Date())
        );
        for (SmShipCrewDetail smShipCrewDetail : detailList) {
            smShipCrewDetail.setStatus("0");
            smShipCrewDetailService.updateById(smShipCrewDetail);
            updateCrewLoadRecord(smShipCrewDetail);
        }
    }

    /**
     * 更新船员上下船状态
     * @param detail
     */
    public void updateCrewLoadRecord(SmShipCrewDetail detail){
        SmShipCrewDetailChange detailChange = new SmShipCrewDetailChange();
        detailChange.setShipCrewId(detail.getShipCrewId());
        detailChange.setCrewName(detail.getCrewName());
        detailChange.setCrewRank(detail.getCrewRank());
        detailChange.setStatus(detail.getStatus());
        detailChange.setUpdatedTime(new Date());
        detailChange.setUpdatedBy(SecurityUtils.getUsername());
        detailChange.setBoatDate(detail.getOnBoatDate());
        detailChange.setBoatPlace(detail.getOnBoatPlace());
        if(StringUtils.equals("0",detailChange.getStatus())){
            detailChange.setBoatDate(detail.getOffBoatDate());
            detailChange.setBoatPlace(detail.getOffBoatPlace());
        }
        smShipCrewDetailChangeService.getBaseMapper().insert(detailChange);
    }

    @Override
    public void resetCrewCountInfo(SmShipCrew smShipCrew) {
        //在船数量
        Long onCount = smShipCrewDetailService.getBaseMapper().selectCount(
                new LambdaQueryWrapper<SmShipCrewDetail>()
                .eq(SmShipCrewDetail::getShipCrewId,smShipCrew.getId())
                .eq(SmShipCrewDetail::getStatus,"1")
                .eq(SmShipCrewDetail::getIsTemp,"0")
        );
        smShipCrew.setCrewNum(onCount.intValue());
        //高级船员数量
        Long highCount = smShipCrewDetailService.getBaseMapper().selectCount(
                new LambdaQueryWrapper<SmShipCrewDetail>()
                        .eq(SmShipCrewDetail::getShipCrewId,smShipCrew.getId())
                        .eq(SmShipCrewDetail::getIsHigh,"1")
                        .eq(SmShipCrewDetail::getIsTemp,"0")
        );
        smShipCrew.setHighCrewNum(highCount.intValue());
        //船长
        SmShipCrewDetail master = smShipCrewDetailService.getBaseMapper().selectOne(
                new LambdaQueryWrapper<SmShipCrewDetail>()
                .eq(SmShipCrewDetail::getShipCrewId,smShipCrew.getId())
                .eq(SmShipCrewDetail::getCrewRank,"Master")
                .eq(SmShipCrewDetail::getStatus,"1")
                .last("limit 1")
        );
        if(master != null){
            smShipCrew.setMasterName(master.getCrewName());
        }
        smShipCrew.setUpdatedTime(new Date());
        smShipCrew.setUpdatedBy(SecurityUtils.getUsername());
        this.baseMapper.updateById(smShipCrew);
    }

    @Override
    public void baseUpdate(SmShipCrew smShipCrew) {
        if(smShipCrew.getId() == null){
            throw new BusException("基础信息异常", BaseResultCode.GENERAL_ERROR);
        }
        SmShipCrew old = this.baseMapper.selectById(smShipCrew.getId());
        if(old == null){
            throw new BusException("基础信息异常", BaseResultCode.GENERAL_ERROR);
        }
        if(StringUtils.isNotBlank(smShipCrew.getShipName())){
            old.setShipName(smShipCrew.getShipName());
        }
        if(StringUtils.isNotBlank(smShipCrew.getImo())){
            old.setImo(smShipCrew.getImo());
        }
        if(StringUtils.isNotBlank(smShipCrew.getCallSign())){
            old.setCallSign(smShipCrew.getCallSign());
        }
        if(StringUtils.isNotBlank(smShipCrew.getFlagState())){
            old.setFlagState(smShipCrew.getFlagState());
        }
        old.setUpdatedTime(new Date());
        old.setUpdatedBy(SecurityUtils.getUsername());
        this.baseMapper.updateById(old);

        //增加操作记录
        SmShipCrewRecord crewRecord = new SmShipCrewRecord();
        crewRecord.setShipCrewId(smShipCrew.getId());
        crewRecord.setOperateType("4");
        crewRecord.setOperateRemark("船只【"+smShipCrew.getShipName()+"】基础信息更新");
        crewRecord.setOperateTime(new Date());
        crewRecord.setOperateBy(SecurityUtils.getUsername());
        smShipCrewRecordService.getBaseMapper().insert(crewRecord);
    }

    @Override
    public SmShipCrew baseDetail(Long shipCrewId) {
        SmShipCrew old = this.baseMapper.selectById(shipCrewId);
        if(old == null){
            throw new BusException("基础信息异常", BaseResultCode.GENERAL_ERROR);
        }
        //获取船员总数
        Long count = smShipCrewDetailService.getBaseMapper().selectCount(
                new LambdaQueryWrapper<SmShipCrewDetail>()
                        .eq(SmShipCrewDetail::getShipCrewId,shipCrewId)
                        .eq(SmShipCrewDetail::getMessagePoint,"4")
        );
        old.setAllCrewCount(count.intValue());
        return old;
    }

    @Override
    public SmShipCrewDetail detailTempCrew(Long shipCrewId) {
        SmShipCrewDetail old = smShipCrewDetailService.getBaseMapper().selectOne(
                new LambdaQueryWrapper<SmShipCrewDetail>()
                .eq(SmShipCrewDetail::getShipCrewId,shipCrewId)
                .eq(SmShipCrewDetail::getIsTemp,"1")
                .eq(SmShipCrewDetail::getCreateBy,SecurityUtils.getUsername())
                .last("limit 1")
        );
        return old;
    }

    /**
     * 校验船员证唯一
     * @param passportNo
     * @return
     */
    public Boolean checkPassPortNo(String passportNo){
        if(StringUtils.isBlank(passportNo)){
            return false;
        }
        Long count = smShipCrewDetailService.getBaseMapper().selectCount(
                new LambdaQueryWrapper<SmShipCrewDetail>()
                        .eq(SmShipCrewDetail::getPassportNo,passportNo)
                        .eq(SmShipCrewDetail::getIsTemp,"0")
        );
        return count > 0 ? true : false;
    }

    @Override
    public JsonResult detailUpdate(SmShipCrewDetail smShipCrewDetail) {
        if(smShipCrewDetail.getId() == null){
            return JsonResult.failed("船员ID不能为空");
        }
        SmShipCrewDetail old = smShipCrewDetailService.getBaseMapper().selectById(smShipCrewDetail.getId());
        if(old == null){
            return JsonResult.failed("基础信息异常");
        }
        String oldCrewName = old.getCrewName();

        Long shipCrewId = old.getShipCrewId();

        if(StringUtils.isNotBlank(smShipCrewDetail.getPassportNo())){
            if(StringUtils.isBlank(old.getPassportNo())
                    || !StringUtils.equals(smShipCrewDetail.getPassportNo(),old.getPassportNo())){
                if(checkPassPortNo(old.getPassportNo())){
                    return JsonResult.failed("船员证号已存在");
                }
            }
        }
        Boolean isChange = false;
        if(!StringUtils.equals(smShipCrewDetail.getStatus(),old.getStatus())){
            isChange = true;
        }
        BeanCopyUtils.copy(smShipCrewDetail,old);
        if(StringUtils.equals(old.getStatus(),"1") && old.getOffBoatDate() != null &&  old.getOffBoatDate().before(new Date())){
            old.setStatus("0");
            isChange = true;
        }
        smShipCrewDetailService.getBaseMapper().updateById(old);

        //更新船舶基础信息操作人信息
        SmShipCrew smShipCrew = this.baseMapper.selectById(shipCrewId);
        if(smShipCrew != null){
            smShipCrew.setUpdatedTime(new Date());
            smShipCrew.setUpdatedBy(SecurityUtils.getUsername());
            //添加船旗国和呼号
            String imo = smShipCrew.getImo();
            if(StringUtils.isNotBlank(imo)){
                IhsShipBase shipBase = informationListMapper.getShipInfoByImo(imo);
                if(null != shipBase){
                    smShipCrew.setCallSign(shipBase.getCallsign());
                    smShipCrew.setFlagState(shipBase.getFlagName());
                }
            }
            this.baseMapper.updateById(smShipCrew);
        }

        //增加操作记录
        SmShipCrewRecord crewRecord = new SmShipCrewRecord();
        crewRecord.setShipCrewId(shipCrewId);
        crewRecord.setOperateType("3");
        crewRecord.setOperateRemark("船员【"+old.getCrewName()+"】信息编辑");
        crewRecord.setOperateTime(new Date());
        crewRecord.setOperateBy(SecurityUtils.getUsername());
        smShipCrewRecordService.getBaseMapper().insert(crewRecord);
        if(isChange){
            updateCrewLoadRecord(old);
        }
        this.resetCrewCountInfo(smShipCrew);

        //更新移动信息里面船员基础信息
        smShipCrewDetailChangeService.update(
                new LambdaUpdateWrapper<SmShipCrewDetailChange>()
                        .set(SmShipCrewDetailChange::getCrewRank,old.getCrewRank())
                        .set(SmShipCrewDetailChange::getCrewName,old.getCrewName())
                        .eq(SmShipCrewDetailChange::getCrewName,oldCrewName)
                        .eq(SmShipCrewDetailChange::getShipCrewId,old.getShipCrewId())
        );


        //更新船员最新一条上下船信息
        SmShipCrewDetailChange detailChange = smShipCrewDetailChangeService.getBaseMapper().selectOne(
                new LambdaQueryWrapper<SmShipCrewDetailChange>()
                    .eq(SmShipCrewDetailChange::getCrewName,old.getCrewName())
                    .eq(SmShipCrewDetailChange::getStatus,old.getStatus())
                    .eq(SmShipCrewDetailChange::getShipCrewId,old.getShipCrewId())
                    .orderByDesc(SmShipCrewDetailChange::getCreateDate)
                    .last("limit 1")
        );
        if(detailChange != null){
            detailChange.setBoatDate(old.getOnBoatDate());
            detailChange.setBoatPlace(old.getOnBoatPlace());
            if(StringUtils.equals("0",old.getStatus())){
                detailChange.setBoatDate(old.getOffBoatDate());
                detailChange.setBoatPlace(old.getOffBoatPlace());
            }
            smShipCrewDetailChangeService.updateById(detailChange);
        }

        return JsonResult.success();
    }

    @Autowired
    @Resource
    ProcessInstanceService processInstanceService;
    @Override
    public void baseDelete(Long shipCrewId) {
        SmShipCrew old = this.baseMapper.selectById(shipCrewId);
        if(old == null){
            throw new BusException("基础信息异常", BaseResultCode.GENERAL_ERROR);
        }

        if(old.getDelApply()!=null && old.getDelApply()==1){
            throw new BusException("当前记录已提交删除申请，请勿重复提交", BaseResultCode.GENERAL_ERROR);
        }
        old.setDelApply(1);
        //开启审批流程
        Map<String,Object> map = new HashMap<>();
        map.put("serviceType",1);
        map.put("module","shipCrew");
        map.put("moduleName","船舶船员信息");
        String processInstanceId = processInstanceService.createProcessInstance2(BpmInstanceService.SYSTEM_DELETE_BPM_KEY,map,shipCrewId+"");
        old.setProcessInstanceId(processInstanceId);
        this.updateById(old);
       /*
        );*/
    }

    @Override
    public void cancelDelete(String businessKey, Integer status) {
        SmShipCrew old = this.baseMapper.selectById(businessKey);
        if(status==-1){
            old.setDelApply(0);
            this.updateById(old);
        }else if(status==1){
            this.baseMapper.deleteById(businessKey);
            smShipCrewDetailService.getBaseMapper().delete(
                    new LambdaQueryWrapper<SmShipCrewDetail>()
                            .eq(SmShipCrewDetail::getShipCrewId,businessKey)
            );
            smShipCrewRecordService.getBaseMapper().delete(
                    new LambdaQueryWrapper<SmShipCrewRecord>()
                            .eq(SmShipCrewRecord::getShipCrewId,businessKey)
            );
            smShipCrewDetailChangeService.getBaseMapper().delete(
                    new LambdaQueryWrapper<SmShipCrewDetailChange>()
                            .eq(SmShipCrewDetailChange::getShipCrewId,businessKey));
        }else if(status==2){
            old.setDelApply(2);
            this.updateById(old);
        }
    }

    /**
     * 格式化excel船员信息
     * @param allRows
     * @return
     */
    public List<SmShipCrewDetail> formatShipCrewExcelData(List<Map<Integer,String>> allRows){
        List<SmShipCrewDetail> resultList = new LinkedList<>();
        for (Map<Integer, String> allRow : allRows) {
            if(allRow.get(2) == null){
                continue;
            }
            SmShipCrewDetail crewDetail = new SmShipCrewDetail();
            for (Integer integer : allRow.keySet()) {
                switch (integer) {
                    case 2:
                        crewDetail.setCrewRank(allRow.get(integer));
                        break;
                    case 3:
                        crewDetail.setCrewName(allRow.get(integer));
                        break;
                    case 4:
                        crewDetail.setCrewBirth(parseDateStr(allRow.get(integer)));
                        break;
                    case 5:
                        crewDetail.setCrewCountry(allRow.get(integer));
                        break;
                    case 6:
                        crewDetail.setPassportNo(allRow.get(integer));
                        break;
                    case 7:
                        crewDetail.setIssueDate(parseDateStr(allRow.get(integer)));
                        break;
                    case 8:
                        crewDetail.setExpiraDate(parseDateStr(allRow.get(integer)));
                        break;
                    case 9:
                        crewDetail.setOnBoatDate(parseDateStr(allRow.get(integer)));
                        break;
                    case 10:
                        crewDetail.setOffBoatDate(parseDateStr(allRow.get(integer)));
                        break;
                    case 11:
                        crewDetail.setOnBoatPlace(allRow.get(integer));
                        break;
                }
            }
            resultList.add(crewDetail);
        }
        return resultList;
    }

    /**
     * 日期格式化
     * @param dateString
     * @return
     */
    public static Date parseDateStr(String dateString){
        //26-Dec-1974
        Date date = null;
        if(dateString.indexOf("-") < 0){
            return date;
        }
        String[] split = dateString.split("-");
        if(split.length < 3){
            return date;
        }
        String year = split[2].length() == 2 ? "20"+split[2] : split[2];
        String month = monthMap.get(split[1]);
        String day = split[0].length() == 1 ? "0"+split[0] : split[0];
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String finalDateStr = (year+"-"+month+"-"+day);
        try {
            date = format.parse(finalDateStr);
        }catch (Exception e){
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        return date;
    }

}




